package com.Action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Category;
import com.Entity.InterestInfo;
import com.Entity.KnowledgeInfo;
import com.Entity.RecNewsListInfo;
import com.Entity.Page.Json;
import com.Service.CategoryServiceI;
import com.Service.ClickInfoService;
import com.Service.InterestInfoService;
import com.Service.KnowledgeServiceI;
import com.Service.RecNewsListInfoService;
import com.util.timerTask.InterestModel;
import com.util.timerTask.TimerTask;

public class KnowledgeAction extends BaseAction {

	private List<InterestInfo> interestlist = new ArrayList<InterestInfo>();
	@Autowired
	private InterestInfoService interestSerivce;
	@Autowired
	private KnowledgeServiceI knowledgeService;
	@Autowired
	private CategoryServiceI categoryService;
	@Autowired
	private RecNewsListInfoService recNewsService;
	@Autowired
	private ClickInfoService clickInfoService;
	private int userid;
	// 定义返回的JSON
	Json json;

	KnowledgeInfo knowledge = new KnowledgeInfo();
	Category category = new Category();
	List<KnowledgeInfo> knowledgelist = new ArrayList<KnowledgeInfo>();

	/**
	 * 获取用户感兴趣标签
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void getInterestTag() throws Exception {
		userid = Integer.valueOf(ServletActionContext.getRequest()
				.getParameter("userId"));
		interestlist = interestSerivce.findByUserId(userid);
		json = new Json();
		json.setSuccess(true);
		json.setMsg("获取用户感兴趣标签成功！");
		json.setObj(interestlist);
		writeJson(json);
	}

	/**
	 * 根据channelId获取对应栏目的医疗信息列表
	 */
	public void getNewsList() throws Exception {
		String text = ServletActionContext.getRequest().getParameter("text");
		String userid = ServletActionContext.getRequest()
				.getParameter("userid");
		int userId = 0;
		if (StringUtils.isNotEmpty(userid)) {
			userId = Integer.valueOf(userid);
		}
		// 转码
		text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
		text = java.net.URLDecoder.decode(text, "UTF-8");

		json = new Json();
		if (!text.equals("推荐")) {
			category = categoryService.findByCategoryName(text);
			knowledgelist = this.knowledgeService.findByCategoryId(category
					.getCategoryId());
			json.setSuccess(true);
			json.setMsg("其他");
			json.setObj(knowledgelist);
		} else {
			List<RecNewsListInfo> recNewsList = new ArrayList<RecNewsListInfo>();
			// 查询当前用户的信息推荐列表
			recNewsList = recNewsService.queryRecNewsList(userId);
			if (recNewsList.isEmpty()) {
				TimerTask.getOrUpdateRecNews();
				List<RecNewsListInfo> recNewsList2 = recNewsService
						.queryRecNewsList(userId);
				for (RecNewsListInfo news : recNewsList2) {
					knowledge = this.knowledgeService.getById(news.getNewsId());
					knowledgelist.add(knowledge);
				}
			} else {
				for (RecNewsListInfo news : recNewsList) {
					knowledge = this.knowledgeService.getById(news.getNewsId());
					knowledgelist.add(knowledge);
				}
			}
			json.setMsg("推荐");
			json.setObj(knowledgelist);
			json.setSuccess(true);
		}
		writeJson(json);
	}

	/**
	 * 根据ID获取资讯详情
	 * 
	 * @throws UnsupportedEncodingException
	 */
	public void getDetailNews() throws Exception {
		Long newsid = Long.valueOf(ServletActionContext.getRequest()
				.getParameter("newsId"));
		Integer userid = Integer.valueOf(ServletActionContext.getRequest()
				.getParameter("userId"));
		knowledge = this.knowledgeService.getById(newsid);
		/*
		 * //查询当前用户的点击行为 List<ClickInfo> clickList=new ArrayList<ClickInfo>();
		 * clickList=this.clickInfoService.findByUserIdAndNewsId(userid,newsid);
		 * if(clickList.isEmpty()){ //保存用户的点击行为 ClickInfo clickInfo=new
		 * ClickInfo(); clickInfo.setClickNum(1); clickInfo.setClickTime(new
		 * java.sql.Date(System.currentTimeMillis()));
		 * clickInfo.setInfoId(newsid); clickInfo.setUserId(userid);
		 * clickInfoService.save(clickInfo); }else{ //更新点击次数 ClickInfo
		 * clickInfo=new ClickInfo(); clickInfo=clickList.get(0);
		 * clickInfo.setClickNum(clickInfo.getClickNum()+1);
		 * clickInfoService.update(clickInfo); }
		 */
		InterestModel.updateInterestModel(userid, newsid);

		json = new Json();
		json.setSuccess(true);
		json.setObj(knowledge);
		json.setMsg("获取资讯内容成功！");
		writeJson(json);
	}

}
